package com.edu.exam.entity.curriculum.questionbank;

import com.edu.exam.enums.system.AttachmentStatus;
import com.edu.exam.enums.system.FileCategory;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.Comment;
import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.Where;

import jakarta.persistence.*;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import java.time.LocalDateTime;

/**
 * 附件信息实体类
 * 所属模块：B-02 题库管理模块
 * 对应数据库表：attachments
 *
 * 管理试题相关的附件文件信息，包括文件存储路径、元数据、访问统计等。
 * 支持试题附件的完整生命周期管理。
 *
 * @author System
 * @version 1.0.0
 */
@Entity
@Comment("附件信息表")
@SQLDelete(sql = "UPDATE attachments SET is_deleted = true, delete_time = CURRENT_TIMESTAMP WHERE id = ?")
@Where(clause = "is_deleted = false")
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode(callSuper = false)
public class Attachment {

    /**
     * 附件唯一标识符
     */
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private Long id;

    /**
     * 试题ID
     */
    @NotNull(message = "试题ID不能为空")
    @Column(name = "question_id", nullable = false)
    private Long questionId;

    /**
     * 原始文件名
     */
    @NotBlank(message = "原始文件名不能为空")
    @Size(max = 255, message = "原始文件名长度不能超过255个字符")
    @Column(name = "original_filename", nullable = false, length = 255)
    private String originalFilename;

    /**
     * 存储文件名
     */
    @NotBlank(message = "存储文件名不能为空")
    @Size(max = 255, message = "存储文件名长度不能超过255个字符")
    @Column(name = "stored_filename", nullable = false, length = 255)
    private String storedFilename;

    /**
     * 文件路径
     */
    @NotBlank(message = "文件路径不能为空")
    @Size(max = 500, message = "文件路径长度不能超过500个字符")
    @Column(name = "file_path", nullable = false, length = 500)
    private String filePath;

    /**
     * 文件大小（字节）
     */
    @NotNull(message = "文件大小不能为空")
    @Column(name = "file_size", nullable = false)
    private Long fileSize;

    /**
     * 文件类型
     */
    @NotNull(message = "文件类型不能为空")
    @Enumerated(EnumType.STRING)
    @Column(name = "file_type", nullable = false, length = 20)
    private FileCategory fileType;

    /**
     * MIME类型
     */
    @Size(max = 100, message = "MIME类型长度不能超过100个字符")
    @Column(name = "mime_type", length = 100)
    private String mimeType;

    /**
     * 文件哈希值
     */
    @NotBlank(message = "文件哈希值不能为空")
    @Size(max = 64, message = "文件哈希值长度不能超过64个字符")
    @Column(name = "file_hash", nullable = false, length = 64)
    private String fileHash;

    /**
     * 上传者ID
     */
    @NotNull(message = "上传者ID不能为空")
    @Size(max = 50, message = "上传者ID长度不能超过50个字符")
    @Column(name = "uploader_id", nullable = false, length = 50)
    private String uploaderId;

    /**
     * 上传者姓名
     */
    @Size(max = 100, message = "上传者姓名长度不能超过100个字符")
    @Column(name = "uploader_name", length = 100)
    private String uploaderName;

    /**
     * 上传时间
     */
    @NotNull(message = "上传时间不能为空")
    @Column(name = "upload_time", nullable = false)
    private LocalDateTime uploadTime;

    /**
     * 访问次数
     */
    @Column(name = "access_count")
    @Builder.Default
    private Integer accessCount = 0;

    /**
     * 下载次数
     */
    @Column(name = "download_count")
    @Builder.Default
    private Integer downloadCount = 0;

    /**
     * 最后访问时间
     */
    @Column(name = "last_access_time")
    private LocalDateTime lastAccessTime;

    /**
     * 附件状态
     */
    @NotNull(message = "附件状态不能为空")
    @Enumerated(EnumType.STRING)
    @Column(name = "status", nullable = false, length = 20)
    private AttachmentStatus status;

    /**
     * 附件描述
     */
    @Size(max = 500, message = "附件描述长度不能超过500个字符")
    @Column(name = "description", length = 500)
    private String description;

    /**
     * 缩略图URL
     */
    @Size(max = 500, message = "缩略图URL长度不能超过500个字符")
    @Column(name = "thumbnail_url", length = 500)
    private String thumbnailUrl;

    /**
     * 预览URL
     */
    @Size(max = 500, message = "预览URL长度不能超过500个字符")
    @Column(name = "preview_url", length = 500)
    private String previewUrl;

    /**
     * 下载URL
     */
    @Size(max = 500, message = "下载URL长度不能超过500个字符")
    @Column(name = "download_url", length = 500)
    private String downloadUrl;

    /**
     * 文件元数据，JSON格式
     */
    @Column(name = "file_metadata", columnDefinition = "TEXT")
    private String fileMetadata;

    /**
     * 是否公开
     */
    @Column(name = "is_public")
    @Builder.Default
    private Boolean isPublic = false;

    /**
     * 是否必需附件
     */
    @Column(name = "is_required")
    @Builder.Default
    private Boolean isRequired = false;

    /**
     * 排序顺序
     */
    @Column(name = "sort_order")
    @Builder.Default
    private Integer sortOrder = 0;

    /**
     * 过期时间
     */
    @Column(name = "expire_time")
    private LocalDateTime expireTime;

    /**
     * 记录创建时间
     */
    @Column(name = "created_time", nullable = false, updatable = false)
    private LocalDateTime createdTime;

    /**
     * 记录最后更新时间
     */
    @Column(name = "updated_time", nullable = false)
    private LocalDateTime updatedTime;

    /**
     * 创建人用户ID
     */
    @Size(max = 50, message = "创建人长度不能超过50个字符")
    @Column(name = "created_by", nullable = false, length = 50)
    private String createdBy;

    /**
     * 最后更新人用户ID
     */
    @Size(max = 50, message = "更新人长度不能超过50个字符")
    @Column(name = "updated_by", length = 50)
    private String updatedBy;

    /**
     * 乐观锁版本号
     */
    @Version
    @Column(name = "version", nullable = false)
    @Builder.Default
    private Integer version = 1;

    /**
     * 软删除标记
     */
    @Column(name = "is_deleted", nullable = false)
    @Builder.Default
    private Boolean isDeleted = false;

    /**
     * 删除时间
     */
    @Column(name = "delete_time")
    private LocalDateTime deleteTime;

    @PrePersist
    protected void onCreate() {
        LocalDateTime now = LocalDateTime.now();
        this.createdTime = now;
        this.updatedTime = now;
        if (this.uploadTime == null) {
            this.uploadTime = now;
        }
    }

    @PreUpdate
    protected void onUpdate() {
        this.updatedTime = LocalDateTime.now();
    }
}